﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AvalonDock;
using System.Reflection;
using System.Data;
using CMISLibrary.Libraries;
using CMISLibrary;
using busDanhMuc;
using BaocaoNPC.common;

namespace NPCUS
{
    /// <summary>
    /// Interaction logic for User.xaml
    /// </summary>
    public partial class User : Page
    {

        private DataSet dsOD_DVI_QLY = new DataSet();
        private DataSet dsSQUAN_TRI = new DataSet();
        string Maloi;

        cls_D_DVI_QLY clsDviqly = new cls_D_DVI_QLY();        

        public User()
        {
            InitializeComponent();
            this.userMenu.OnCreateWindow += new NPCUS.OnCreateWindow_Delegate(this.CreateDocumentWindow);

            Load_OD_DVI_QLY();
            load_SQUAN_TRI();
            string datelv = System.DateTime.Now.Day + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.Year;
            txtuser.Text = "Đơn vị: " + NPCUS_DanhMuc.Pglobal.strTen_dviqly + " - Người dùng: " + NPCUS_DanhMuc.Pglobal.strTen_dangnhap + " - Ngày làm việc: " + datelv;
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            while (this.NavigationService.CanGoBack == true)
            {
                this.NavigationService.RemoveBackEntry();
            }
        }

        private void Logout(object sender, RoutedEventArgs e)
        {

        }

        private void ChangeProfile(object sender, RoutedEventArgs e)
        {

        }

        private void Help(object sender, RoutedEventArgs e)
        {

        }

        private void Version(object sender, RoutedEventArgs e)
        {

        }

        public void CreateDocumentWindow(object sender, RoutedEventArgs e, string windowName, string windowTitle, string img, string AssemblyName, string UserControlName)
        {
            try
            {
                //if (MainWorkspace.Contains(windowName) == false)
                //{
                //    Grid grd = new Grid();
                //    grd.Margin = new Thickness(0, 0, 0, 0);

                //    ScrollViewer scv = new ScrollViewer();
                //    scv.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                //    scv.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

                //    StackPanel stk = new StackPanel();
                //    Assembly asm;
                //    System.Windows.Controls.UserControl usc = new System.Windows.Controls.UserControl();
                //    asm = System.Reflection.Assembly.Load(AssemblyName);
                //    usc = (System.Windows.Controls.UserControl)asm.CreateInstance(UserControlName);
                //    usc.Margin = new Thickness(0, 0, 0, 0);

                //    stk.Children.Add(usc);
                //    scv.Content = stk;
                //    grd.Children.Add(scv);

                //    DocumentContent item = new DocumentContent();
                //    MainWorkspace.Items.Add(item);
                //    item.Content = grd;
                //    item.Title = windowTitle;
                //    item.Name = windowName;
                //    item.Background = Brushes.White;
                //    item.Icon = img;

                //    MainDocking.ActiveDocument = item;
                //}
                this.Cursor = Cursors.Wait;
                for (Int16 i = Convert.ToInt16(this.MainWorkspace.Items.Count - 1); i >= 0; i--)
                {
                    DocumentContent item = (DocumentContent)this.MainWorkspace.Items[i];
                    if (item.Title == windowTitle && item.Name == windowName)
                    {
                        item.Focus();
                    }
                }
                if (MainWorkspace.Contains(windowName) == false)
                {
                    Grid grd = new Grid();
                    grd.Margin = new Thickness(0, 0, 0, 0);

                    //ScrollViewer scv = new ScrollViewer();
                    //scv.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                    //scv.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

                    //StackPanel stk = new StackPanel();
                    Assembly asm;
                    UserControl usc = new UserControl();
                    asm = System.Reflection.Assembly.Load(AssemblyName);
                    usc = (UserControl)asm.CreateInstance(UserControlName);
                    usc.Margin = new Thickness(0, 0, 0, 0);
//tungnd.ictnpc: //stk.Children.Add(usc);
                    //scv.Content = stk;
                    //grd.Children.Add(scv);
                    grd.Children.Add(usc);

                    DocumentContent item = new DocumentContent();
                    MainWorkspace.Items.Add(item);
                    item.Content = grd;
                    item.Title = windowTitle;
                    item.Name = windowName;
                    item.Background = Brushes.White;
                    item.Icon = img;
                    //item.OutlookSectionNavigation += new OnOutlookSectionNavigation_Delegate(this.OutlookSectionNavigation);

                    MainDocking.ActiveDocument = item;
                }
                this.Cursor = null;

            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi khi khởi tạo chức năng : " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        private void Load_OD_DVI_QLY()
        {
            dsOD_DVI_QLY = GlobalServices.Service_NPCUS.get_D_DVIQLY(NPCUS_DanhMuc.Pglobal.strMa_dviqly);
            //dsOD_DVI_QLY = clsDviqly.get_D_DVIQLY(NPCUS_DanhMuc.Pglobal.strMa_dviqly);
            cboMA_DVIQLY.DataContext = dsOD_DVI_QLY.Tables[0].DefaultView;
            cboMA_DVIQLY.DisplayMemberPath = dsOD_DVI_QLY.Tables[0].Columns["TEN_DVIQLY"].ToString();
            cboMA_DVIQLY.SelectedValuePath = dsOD_DVI_QLY.Tables[0].Columns["MA_DVIQLY"].ToString();
            //NPCUS_DanhMuc.Pglobal.strMa_dviqly = cls._strMa_dviqly;

        }

        private void load_SQUAN_TRI()
        {
            dsSQUAN_TRI = GlobalServices.Service_NPCUS.get_SQUAN_TRI(NPCUS_DanhMuc.Pglobal.strMa_dviqly, ref Maloi);
            //dsSQUAN_TRI = clsDviqly.get_SQUAN_TRI(NPCUS_DanhMuc.Pglobal.strMa_dviqly, ref Maloi);
            if (dsSQUAN_TRI != null)
            {
                NPCUS_DanhMuc.Pglobal.iThang = Convert.ToInt16(dsSQUAN_TRI.Tables[0].Rows[0]["THANG"]);
                NPCUS_DanhMuc.Pglobal.iNam = Convert.ToInt16(dsSQUAN_TRI.Tables[0].Rows[0]["NAM"]);
                lblThangNamLamViec.Content = NPCUS_DanhMuc.Pglobal.iThang + " / " + NPCUS_DanhMuc.Pglobal.iNam;

            }
            else
            {
                //MessageBox.Show(Maloi, "Thông báo", MessageBoxButton.OK);
            };
        }

        private void btnMA_DVIQLY_Click(object sender, RoutedEventArgs e)
        {

            NPCUS_DanhMuc.Pglobal.strMa_dviqly = cboMA_DVIQLY.SelectedValue.ToString();
            NPCUS_DanhMuc.Pglobal.strTen_dviqly = ((DataRowView)cboMA_DVIQLY.SelectedItem).Row["TEN_DVIQLY"].ToString();

            cls_D_DVI_QLY clsdviqly = new cls_D_DVI_QLY();
            DataSet ds = new DataSet();
            //ds = clsdviqly.select_D_DVI_QLY_captren(NPCUS_DanhMuc.Pglobal.strMa_dviqly);
            ds = GlobalServices.Service_NPCUS.select_D_DVI_QLY_captren(NPCUS_DanhMuc.Pglobal.strMa_dviqly);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT = ds.Tables[0].Rows[0]["TEN_DVIQLY"].ToString();
            }
            else
            {
                NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT = "";
            }

            load_SQUAN_TRI();
            MessageBox.Show("Bạn đã chọn làm việc với Đơn vị: '" + cboMA_DVIQLY.Text.ToString() + " '", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
            if (this.MainWorkspace.Items.Count > 0)
            {
                for (Int32 i = this.MainWorkspace.Items.Count; i > 0; i--)
                {
                    this.MainWorkspace.Items.Remove(this.MainWorkspace.Items[i - 1]);
                }
            }
            string datelv = System.DateTime.Now.Day + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.Year;
            txtuser.Text = "Đơn vị: " + cboMA_DVIQLY.Text.ToString() + " - Người dùng: " + NPCUS_DanhMuc.Pglobal.strTen_dangnhap + " - Ngày làm việc: " + datelv;
        }

        private void btnThongTin_Click(object sender, RoutedEventArgs e)
        {
            CreateDocumentWindow(sender, e, "QUANLYNGUOIDUNG", "Quản lý Thông tin người dùng", "/NPCUS;component/Images/window.png", "NPCUS_DanhMuc", "NPCUS_DanhMuc.Usercontrols.QUANLYNGUOIDUNG");
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/NPCUS;component/pages/Login.xaml", UriKind.Relative));
        }





    }
}
